package tt.dz.order.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

public class UUIDUtil {
	private static int digitNum = 4;
	
	public static long longUUID(){
		return Math.abs(UUID.randomUUID().getMostSignificantBits());
	}
	
	public static String shortUUID(){
        long uuid = longUUID();
        char[] uuidStr = String.valueOf(uuid).toCharArray();
        StringBuilder shortUUIDStr = new StringBuilder(digitNum);
        int i = 0;
        for(i = 0; i < digitNum; i++){
        	shortUUIDStr.append(uuidStr[i]);
        }
		return shortUUIDStr.toString();
	}
	
	/**
	 * 获取14位当前时间
	 * @return 14位当前时间
	 */
	public static String getCurrentDate14(Date now){
		if(now == null) now = new Date();
		return new SimpleDateFormat("yyyyMMddHHmmss").format(now);
	}
	
	/**
	 * 获取当前时间
	 */
	public static String getCurrentDate(Date now, String dataFormat){
		if(now == null) now = new Date();
		if(dataFormat == null || dataFormat.length() == 0) dataFormat = "yyyyMMddHHmmss";
		return new SimpleDateFormat(dataFormat).format(now);
	}
	
	/**
	 * 生成n位随机数
	 * @param n 位数
	 * @return
	 */
	public static String randomNum(int n){
		StringBuilder builder = new StringBuilder(n);  
	    for(int i = 0; i < n; i++) {
			Random rnd = new Random(longUUID());
			builder.append((char)('0' + rnd.nextInt(10)));
	    }
		return builder.toString();
	}
	
	public static long getId(String tableNo){
		String now = getCurrentDate14(null);
		StringBuilder sbuilder = new StringBuilder(tableNo);
		sbuilder.append(now).append(shortUUID());
		return Long.valueOf(sbuilder.toString());
	}

	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();
		System.out.println(c.get(Calendar.YEAR));
		System.out.println(c.get(Calendar.MONTH));
		System.out.println(c.get(Calendar.DATE));
		System.out.println(c.get(Calendar.HOUR_OF_DAY));
		System.out.println(c.get(Calendar.MINUTE));
		System.out.println(c.get(Calendar.SECOND));
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		System.out.println(df.format(new Date()));
		Set<String> set = new HashSet<String>();
		for(int j = 0; j < 1000; j++){
			String rNum = randomNum(6);
			set.add(rNum);
			System.out.println(rNum);
		}
		System.out.println(set.size());
		/*
		int i = 0;
		for(i = 0; i < 10000; i++){
			set.add(shortUUID());
		}
		System.out.println(set.size());
		*/
	}

}
